#!/usr/bin/python
import pygame
from pygame.locals import *
from pygame.font import Font
from pygame.sprite import Sprite

import os
import sys
import random

class DemoSprite(Sprite):

	def __init__(self):
		self.frames = [None, None] #only 2 frames
		self.curFrame = None

		#motion
		self.zoom = 1

		#animation
		self.lastAnimate     = 0
		self.animateSpeed    = 50

		f1 = os.path.join('.', 'walk_right_1.png')
		self.frames.insert(0, pygame.image.load(f1))

		self.nextFrame() #set the curFrame to frames[0]

	def nextFrame(self):
		#toggle between 2 frames
		if (self.curFrame is self.frames[0]):
			self.curFrame = self.frames[1]
			return
		self.curFrame = self.frames[0]

	def update(self, world):
		Sprite.update(self)  #call the parent method
		if (self.zoom > 0):
			self.zoomOut(world)
		if (self.zoom < 0):
			self.zoomIn(world)


		#bob 
		rect = self.curFrame.get_rect()
		if (rect[3] < 100 ):
			self.zoom = -1
		if (rect[3] > 300 ):
			self.zoom = 1

	def animate(self, world):
		self.lastAnimate  += world.getDeltaTic()
		if (self.lastAnimate > self.animateSpeed):
			self.nextFrame()
			self.lastAnimate = 0

	def zoomOut(self, world):
		scalingFactor = 4
		self.lastAnimate  += world.getDeltaTic()
		if (self.lastAnimate > self.animateSpeed):
			rect = self.curFrame.get_rect()

			self.curFrame = pygame.transform.smoothscale(self.frames[0], (rect[2] - scalingFactor,rect[3] - scalingFactor) )
			self.lastAnimate = 0

	def zoomIn(self, world):
		scalingFactor = 4

		self.lastAnimate  += world.getDeltaTic()
		if (self.lastAnimate > self.animateSpeed):
			rect = self.curFrame.get_rect()

			self.curFrame = pygame.transform.smoothscale(self.frames[0], (rect[2] + scalingFactor,rect[3] + scalingFactor) )
			self.lastAnimate = 0

	def paint(self, g):
			g.blit( self.curFrame, (0,0) )

class GameWorld():

	def __init__(self, window):
		self.window = window
		self.charSprite = None #only 1 character sprite

		self.deltaTic = 0  #keep track of ticks per frame
		self.clock = pygame.time.Clock()

		self.isDead = 0 #game over

		self.wallpaper = pygame.Surface( (640, 480), 0, None )
		self.wallpaper.fill( (200,200,200), pygame.Rect(0, 0, 640, 480))
	
	def setCharSprite(self, s):
		self.charSprite = s

	def gameLoop(self):
		while (1):
			#only call this once per game loop
			self.deltaTic = self.clock.tick(40)

			evt = pygame.event.poll()
			pygame.event.clear()
			self.onKey(evt)
			if self.isDead:
				#this ends the while(1) loop
				break;

			#erase old position (should be handled by dirty rec
			self.window.blit(self.wallpaper, (0,0))
			#paint the screen
			self.charSprite.paint(self.window)
			pygame.display.update()


	def getDeltaTic(self):
		return self.deltaTic

	def onKey(self, evt):
		if (evt.type == QUIT or (evt.type == KEYDOWN and evt.key == K_ESCAPE) ):
			print 'got quit event'
			self.isDead = 1

		if (evt.type == KEYDOWN and evt.key == K_RIGHT):
			self.charSprite.h_velocity = 1
			self.charSprite.is_accelerating = 1
		if (evt.type == KEYUP and (evt.key == K_RIGHT)):
			self.charSprite.h_velocity=0

		self.charSprite.update(self) #give the sprite access to the gameworld

def startDemo():
	"""start a window, load up the main sprite"""

	pygame.init()
	pygame.display.init()
	window = pygame.display.set_mode( (640,480), RESIZABLE )
	pygame.display.set_caption('Animate Demo')

	sprite =  DemoSprite()
	world =  GameWorld(window)
	world.setCharSprite(sprite)
	#start the simulation
	world.gameLoop()
	#game is done here


if __name__ == '__main__' :
	startDemo();
